package com.cskaoyan.service.wx_brand_service;

import com.cskaoyan.bean.Brand;
import com.cskaoyan.bean.BrandExample;
import com.cskaoyan.bean.wx_vo.brand.SingleWxBrandVO;
import com.cskaoyan.bean.wx_vo.brand.WxBrandDetailVO;
import com.cskaoyan.bean.wx_vo.brand.WxBrandInfoVO;
import com.cskaoyan.mapper.BrandMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @description：
 * @author: 曹旭
 * @date: 2022/5/9 17:26
 */
@Service
public class WxBrandServiceImpl implements WxBrandService{
    @Autowired
    BrandMapper brandMapper;

    @Override
    public WxBrandInfoVO list(Integer page, Integer limit) {
        // 分页
        PageHelper.startPage(page, limit);
        // 查询
        BrandExample example = new BrandExample();
        List<Brand> brands = this.brandMapper.selectByExample(example);
        // 获得分页信息
        PageInfo<Brand> pageInfo = new PageInfo(brands);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        // 封装结果
        ArrayList<SingleWxBrandVO> singleWxBrandVOS = new ArrayList<>();
        for (Brand brand : brands) {
            SingleWxBrandVO singleWxBrandVO = SingleWxBrandVO.builder().id(brand.getId()).desc(brand.getDesc())
                    .floorPrice(brand.getFloorPrice()).name(brand.getName())
                    .picUrl(brand.getPicUrl()).build();
            singleWxBrandVOS.add(singleWxBrandVO);
        }
        WxBrandInfoVO wxBrandInfoVO = WxBrandInfoVO.builder().list(singleWxBrandVOS).limit(limit)
                .page(page).pages(pages).total(total).build();
        return wxBrandInfoVO;
    }

    @Override
    public WxBrandDetailVO detail(Integer id) {
        Brand brand = this.brandMapper.selectByPrimaryKey(id);
        // POJO转VO
        WxBrandDetailVO wxBrandDetailVO = WxBrandDetailVO.builder().addTime(brand.getAddTime()).deleted(brand.getDeleted())
                .desc(brand.getDesc()).floorPrice(brand.getFloorPrice())
                .id(brand.getId()).name(brand.getName()).picUrl(brand.getPicUrl())
                .sortOrder(brand.getSortOrder()).updateTime(brand.getUpdateTime()).build();
        return wxBrandDetailVO;
    }
}
